<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>变形绘制</title>
  <style>
    canvas {
      border: 1px solid #000;
    }
  </style>
</head>

<body>
  <canvas id="myCanvas" width="600" height="500"></canvas>
  <script>
    const canvas = document.getElementById("myCanvas");
    const ctx = canvas.getContext("2d");

    // 保存当前canvas状态
    ctx.save();
    ctx.translate(50, 50);
    ctx.fillStyle = "#ccc";
    ctx.fillRect(0, 0, 120, 120);

    // 恢复: 恢复到上一次保存的canvas画布状态 也就是没有被移动过的画布
    ctx.restore();
    ctx.fillStyle = "#08a2f1";
    ctx.fillRect(0, 0, 100, 100);

    // TODO: 变形注意事项, 变形之前记得备份，将原状态进行备份，然后变形，变形完毕后一定要恢复到原始状态，避免影响下一次的改变
  </script>
</body>

</html>